package Controller;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import Entity.Project;
import Model.ProjectManageModel;
import View.ProjectManageView;

public class ProjectManageController {
	private boolean update = false;
	private ProjectManageView view;
	private ProjectManageModel model;
	private JFrame frame;

	public ProjectManageController(){
		super();
		frame = new JFrame();
		view = new ProjectManageView();
		model = new ProjectManageModel();
		view.setModel(model);
		doAction();
		frame.add(view);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		frame.setBounds(screensize.width/2-400,screensize.height/2 - 300,800,600);
		frame.setVisible(true);
	}

	private void doAction() {
		// TODO Auto-generated method stub
		doAdd();
		doWrite();
		doEdit();
		doDelete();
		doExit();
		loadTable();
		doSelectTable();
	}
	
	private void loadTable() {
		// TODO Auto-generated method stub
		try {
			model.loadProjectList();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doSelectTable() {
		// TODO Auto-generated method stub
		view.doSelectTable(new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				doSelect();
			}

		});
	view.doSelectTable(new MouseAdapter() {

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			doSelect();
		}

	});
}
	private void doSelect() {
		int row = view.getSelectedItem();
		Project project = model.getProjectList().get(row);
		view.display(project);
		view.setEnableControl(false);
	}
	
	private void doAdd() {
		// TODO Auto-generated method stub
		view.doAdd(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
					Project project = view.getData();
					int row = view.getSelectedItem();
					if (project.getName().trim().equals("")) {
						JOptionPane.showMessageDialog(frame,
								"Ban chua nhap ten cho Project");
						return;
					}
					
					if(update){
							try {
								model.editProject(row, project);
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (ClassNotFoundException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
					}
					else {
						try {
							model.addProject(project);						
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (ClassNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					view.setEnableControl(false);
			}
		});	
		view.setEnableControl(false);
	}

	private void doWrite() {
		// TODO Auto-generated method stub
		view.doWrite(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				view.display(new Project("",0));
				view.setEnableControl(true);
			}

		});

	}

	private void doEdit() {
		// TODO Auto-generated method stub
		view.doEdit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//int row = view.getSelectedItem();
				//doSelect();
				update = true;
				view.setEnableControl(true);
			}

		});
	}

	private void doDelete() {
		// TODO Auto-generated method stub
		view.doDelete(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int row = view.getSelectedItem();
				Project project = view.getData();
				try {
					model.deleteProject(row, project);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		});
	}

	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				frame.dispose();
			}

		});
	}

	public static void main(String[] arg) throws IOException, ClassNotFoundException, SQLException  {
			new ProjectManageController();
		}
}
